<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Wilead\ApiBundle\Controller;

use FOS\RestBundle\Controller\Annotations\RequestParam;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use FOS\RestBundle\View\RouteRedirectView;

/**
 * Description of UploadController
 *
 * @author jack
 */
class UploadController extends ApiController
{

    /**
     * @ApiDoc(
     *      description="图片上传"
     * )
     * @RequestParam(name="file", requirements="", description="图片文件")
     */
    public function imagesAction()
    {
        set_time_limit(0);
        $handle = $this->getRequest()->files->getIterator()->current();

        /* @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */
        $file = $handle['file'];
        $fileName = uniqid() . '.' . $file->getClientOriginalExtension();
        $savePath = DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . date('Y-m');
        $movePath = $this->container->getParameter('kernel.root_dir') . '/../web' . $savePath;
        $file->move($movePath, $fileName);

        //if (strpos(strtolower($file->getClientMimeType()), 'image') !== FALSE) {
        $avalancheService = $this->get('imagine.cache.path.resolver');

        $array = array(
            'thumbnail_src' => $avalancheService->getBrowserPath($savePath . DIRECTORY_SEPARATOR . $fileName, 'thumbnail'),
            'src' => $savePath . DIRECTORY_SEPARATOR . $fileName
        );
//        } else {
//            $array = array(
//                'src' => $savePath . DIRECTORY_SEPARATOR . $fileName
//            );
//        }

        $view = $this->getFOSView();
        $view->setData($array);
        return $view;
    }

}

?>
